<?php
namespace app\back\controller;

use app\back\model\ConfigModel;
use think\Request;


///系统配置控制器
class System extends AdminCommon
{


    ///基本配置列表
    public function base(ConfigModel $configModel){
        $this->assign([
            'indexDataList'=>$configModel->getIndexDataListByPosition('comm'),
        ]);
        return view();
    }

    ///基本配置修改页面
    public function baseEdit(Request $request,ConfigModel $configModel){
        $config_position = 'comm';
        ///渲染页面
        $this->assign(
            $this->getEditDataComm($request,$configModel,$config_position)
        );
        return view();
    }

    ///基本配置修改
    public function baseEditPost(Request $request,ConfigModel $configModel){
        $config_position = 'comm';
        if(!$request->isAjax()){return think_json_encode(['status'=>2,'msg'=>'修改配置已关闭，请谅解！']);}
        return $this->updateConfigDataBridging($request,$configModel,$config_position);
    }





    ///SEO配置列表
    public function seo(ConfigModel $configModel){
        $this->assign([
            'indexDataList'=>$configModel->getIndexDataListByPosition('seo'),
        ]);
        return view();
    }
    ///SEO配置修改页面
    public function seoEdit(Request $request,ConfigModel $configModel){
        $config_position = 'seo';
        ///渲染页面
        $this->assign(
            $this->getEditDataComm($request,$configModel,$config_position)
        );
        return view();
    }
    ///SEO配置修改
    public function seoEditPost(Request $request,ConfigModel $configModel){
        $config_position = 'seo';
        if(!$request->isAjax()){return think_json_encode(['status'=>2,'msg'=>'修改配置已关闭，请谅解！']);}
        return $this->updateConfigDataBridging($request,$configModel,$config_position);
    }












    ///获取配置数据公共方法
    final public function getEditDataComm(Request $request,ConfigModel $configModel,$config_position){
        $data_id = $request->param('id',0,'intval');
        $config_data = $configModel->getOneDataByIDAndPosition($data_id,$config_position);
        return [
            'config_data'=>$config_data,
            'json_config_data'=>think_json_encode($config_data),
        ];
    }
    ///修改配置数据公共方法
    final public function updateConfigDataComm(Request $request,ConfigModel $configModel,$config_position){
        $config_data = $request->param(false);
        $config_value = $config_data['config_value'] ?? '';
        $config_id = $config_data['id'] ?? 0;
        unset($config_data);


        $config_value = addslashes(htmlspecialchars($config_value,ENT_QUOTES));
        $config_id = intval($config_id);
        $update_time = date('Y-m-d H:i:s');


        ///操作数据库
        try{
            ///修改数据
            return $configModel->updateDataByIDAndPosition($config_id,$config_position,$config_value,$update_time);
        }catch(\Exception $e){
            trace($e->getMessage(),'error');
            return -1;
        }
    }
    ///修改配置数据结果状态转换可理解的提示语方法
    final public function updateConfigDataBridging(Request $request,ConfigModel $configModel,$config_position){
        $edit_res = $this->updateConfigDataComm($request,$configModel,$config_position);
        switch($edit_res){
            case 1:return think_json_encode(['status'=>0,'msg'=>'修改成功！']);break;
            case -1:return think_json_encode(['status'=>2,'msg'=>'修改出现异常，请检查啊！']);break;
            default:return think_json_encode(['status'=>2,'msg'=>'修改失败,请检查是否没有操作！']);
        }
    }







}